package cn.iocoder.yudao.module.iot.service.deviceeventslog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.deviceeventslog.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceeventslog.DeviceEventsLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.iot.convert.deviceeventslog.DeviceEventsLogConvert;
import cn.iocoder.yudao.module.iot.dal.mysql.deviceeventslog.DeviceEventsLogMapper;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.DEVICE_EVENTS_LOG_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 设备事件日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceEventsLogServiceImpl implements DeviceEventsLogService {

    @Resource
    private DeviceEventsLogMapper deviceEventsLogMapper;

    @Override
    public Long createDeviceEventsLog(DeviceEventsLogCreateReqVO createReqVO) {
        // 插入
        DeviceEventsLogDO deviceEventsLog = DeviceEventsLogConvert.INSTANCE.convert(createReqVO);
        deviceEventsLogMapper.insert(deviceEventsLog);
        // 返回
        return deviceEventsLog.getId();
    }

    @Override
    public void updateDeviceEventsLog(DeviceEventsLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateDeviceEventsLogExists(updateReqVO.getId());
        // 更新
        DeviceEventsLogDO updateObj = DeviceEventsLogConvert.INSTANCE.convert(updateReqVO);
        deviceEventsLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceEventsLog(Long id) {
        // 校验存在
        validateDeviceEventsLogExists(id);
        // 删除
        deviceEventsLogMapper.deleteById(id);
    }

    private void validateDeviceEventsLogExists(Long id) {
        if (deviceEventsLogMapper.selectById(id) == null) {
            throw exception(DEVICE_EVENTS_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DeviceEventsLogDO getDeviceEventsLog(Long id) {
        return deviceEventsLogMapper.selectById(id);
    }

    @Override
    public List<DeviceEventsLogDO> getDeviceEventsLogList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return deviceEventsLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DeviceEventsLogDO> getDeviceEventsLogPage(DeviceEventsLogPageReqVO pageReqVO) {
        return deviceEventsLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeviceEventsLogDO> getDeviceEventsLogList(DeviceEventsLogExportReqVO exportReqVO) {
        return deviceEventsLogMapper.selectList(exportReqVO);
    }

}
